/*
 * LoginPage.java
 *
 * Created on 1 2007, 12:35
 */

package server.authentification.module.actions;

import com.persistent.entity.Authentification;
import com.persistent.model.facade.AuthentificationFacade;
import java.io.IOException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import server.authentification.module.WebCallBack;
import server.utils.Chat;
import server.utils.ServiceLocator;
/**
 *
 * @author alexog
 * @version
 */

public class LoginPage extends MappingDispatchAction {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success.login";
    private final static String REJECT = "rejected.login";
    
    /**
     * Class Logger
     */
    private static final Logger log;
    
    static {
        log = Logger.getLogger(server.authentification.module.actions.LoginPage.class);
    }
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward login(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        WebCallBack webcallback = new WebCallBack(request);
        LoginContext lcontext = null;

        boolean loginSuccess = true;
        try{
            lcontext = new LoginContext("TankUserLogin",webcallback);
            lcontext.login();
        }catch (LoginException lge){
            log.debug(lge.getLocalizedMessage()+lge.getMessage());
            loginSuccess = false;
            return mapping.findForward(SUCCESS);
        }
        
        return mapping.findForward(SUCCESS);
        
    }
    
    public ActionForward unspecified(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        //TODO: Remove it
        mapping.findForward("page-not-found");
        return null;
    }
}
